'use strict';
/**
 * Created by FreezeSoul on 2016/1/22.
 */
define([
    "app/core/base",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'echarts',
], function (Chart, $, _, JSON, log, echarts) {

    var echart3 = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._chart = null;
            this._container = null;
            this._option = {
                Binding: [],
                Events: [],
                Extend: {},
                Option: {}
            };
        },
        init: function (element) {
            var _this = this;
            var deferred = $.Deferred();
            require(['libs/echarts/dist/extension/dataTool'], function () {
                try {
                    _this._container = element;
                    _this._chart = echarts.init(element, null, {
                        renderer: 'canvas'
                    });
                    deferred.resolve(_this._chart);
                } catch (e) {
                    log.error(e);
                }
            });
            return deferred.promise();

        },
        _render: function () {
            var _this = this;
            $.get('app/plugins/demo/echart3-graph/les-miserables.xml', function (xml) {
                var graph = echarts.dataTool.gexf.parse(xml);
                var categories = [];
                for (var i = 0; i < 9; i++) {
                    categories[i] = {
                        name: '类目' + i
                    };
                }
                graph.nodes.forEach(function (node) {
                    node.itemStyle = null;
                    node.value = node.symbolSize;
                    if (!node.label) node.label = {};
                    if (!node.label.normal) node.label.normal = {};
                    node.label.normal.show = node.symbolSize > 30;
                    node.category = node.attributes['modularity_class'];
                });
                _this._chart.setOption({
                    tooltip: {},
                    legend: [{
                        data: categories.map(function (a) {
                            return a.name;
                        })
                    }],
                    visualMap: {
                        max: 100,
                        inRange: {
                            colorSaturation: [1, 0.3]
                        }
                    },
                    animationDurationUpdate: 1500,
                    animationEasingUpdate: 'quinticInOut',
                    series: [
                        {
                            name: 'Les Miserables',
                            type: 'graph',
                            layout: 'circular',
                            data: graph.nodes,
                            links: graph.links,
                            categories: categories,
                            roam: true,
                            label: {
                                normal: {
                                    position: 'right'
                                }
                            },
                            lineStyle: {
                                normal: {
                                    curveness: 0.3
                                }
                            }
                        }
                    ]
                });
            });
        },
        example: function () {
            try {
                this._render();
            } catch (e) {
                log.error(e);
            }
        },
        setData: function (data) {
            try {
                this._render();
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
                this._chart.resize();
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                this._chart.dispose();

            } catch (e) {
                log.error(e);
            }
        },
        getChart: function () {
            return this._chart;
        }
    });

    return echart3;
});